package auth;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import lib_model.Account.Account;
import lib_model.Orther.ReturnObj;

/**
 * Servlet implementation class Register
 */
@WebServlet("/auth/ChangePassword")
public class ChangePassword extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ChangePassword() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			String crpw = request.getParameter("txtCrpw");
			String npw = request.getParameter("txtNpw");
			String cfpw = request.getParameter("txtCfpw");
			RequestDispatcher dis = request
					.getRequestDispatcher("../pages/change-password.jsp");
			if (crpw != null && npw != null && cfpw != null ) {
				HttpSession s = request.getSession();
				Account acc = (Account) request.getSession().getAttribute(
						"User");
				if (!acc.password.equals(crpw))
					request.setAttribute("Err",
							"Current Password Not Validation !");
				else if (!npw.equals(cfpw))
					request.setAttribute("Err",
							"Comfirm Password Not Validation !");
				else {
					acc.password = npw;
					ReturnObj objReturn = acc.Update();
					if (objReturn.Result == true) {
						s.setAttribute("User", acc);
						request.setAttribute("Err", "Change-Password Suscess !");
					} else {
						request.setAttribute("Err", "Register fail");
					}
				}
			} else {
				request.setAttribute("Err", "Data is not empty ");
			}
			dis.forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
